package com.example.blog_system.controller;

import com.example.blog_system.dto.ApiResponse;
import com.example.blog_system.dto.ArticleDto;
import com.example.blog_system.dto.CommentDto;
import com.example.blog_system.security.UserPrincipal;
import com.example.blog_system.service.ArticleService;
import com.example.blog_system.service.CommentService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    /**
     * 发布文章
     */
    @PostMapping
    public ResponseEntity<ApiResponse<ArticleDto>> createArticle(
            @Valid @RequestBody ArticleDto articleDto,
            @AuthenticationPrincipal UserPrincipal currentUser) {
        articleDto.setAuthorId(currentUser.getId());
        ArticleDto createdArticle = articleService.createArticle(articleDto, currentUser.getId());
        return ResponseEntity.ok(ApiResponse.success("文章创建成功", createdArticle));
    }

    /**
     * 编辑文章
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<ArticleDto>> updateArticle(
            @PathVariable Long id,
            @Valid @RequestBody ArticleDto articleDto,
            @AuthenticationPrincipal UserPrincipal currentUser) {
        articleDto.setId(id);
        articleDto.setAuthorId(currentUser.getId());
        ArticleDto updatedArticle = articleService.updateArticle(id, articleDto, currentUser.getId());
        return ResponseEntity.ok(ApiResponse.success("文章更新成功", updatedArticle));
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteArticle(
            @PathVariable Long id,
            @AuthenticationPrincipal UserPrincipal currentUser) {
        articleService.deleteArticle(id, currentUser.getId());
        return ResponseEntity.ok(ApiResponse.success("文章删除成功"));
    }

    /**
     * 获取文章详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<ArticleDto>> getArticle(@PathVariable Long id) {
        ArticleDto article = articleService.getArticleById(id);
        return ResponseEntity.ok(ApiResponse.success("获取文章成功", article));
    }

    /**
     * 获取文章列表
     */
    @GetMapping
    public ResponseEntity<ApiResponse<Page<ArticleDto>>> getAllArticles(
            @PageableDefault(page = 0, size = 10, sort = "createdAt", direction = Sort.Direction.DESC) 
            Pageable pageable) {
        Page<ArticleDto> articles = articleService.getAllArticles(pageable);
        return ResponseEntity.ok(ApiResponse.success("获取文章列表成功", articles));
    }

    /**
     * 发表评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<ApiResponse<CommentDto>> createComment(
            @PathVariable Long id,
            @Valid @RequestBody CommentDto commentDto,
            @AuthenticationPrincipal UserPrincipal currentUser
    ) {
        commentDto.setArticleId(id);
        commentDto.setAuthorId(currentUser.getId());
        CommentDto createdComment = commentService.createComment(commentDto, currentUser.getId());
        return ResponseEntity.ok(ApiResponse.success("评论发表成功", createdComment));
    }

    /**
     * 获取用户文章列表
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<ApiResponse<Page<ArticleDto>>> getArticlesByAuthor(
            @PathVariable Long userId,
            @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
        Page<ArticleDto> articles = articleService.getArticlesByAuthor(userId, pageable);
        return ResponseEntity.ok(ApiResponse.success("获取用户文章列表成功", articles));
    }

    /**
     * 搜索文章
     */
    @GetMapping("/search")
    public ResponseEntity<ApiResponse<Page<ArticleDto>>> searchArticles(
            @RequestParam String keyword,
            @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
        Page<ArticleDto> articles = articleService.searchArticles(keyword, pageable);
        return ResponseEntity.ok(ApiResponse.success("搜索文章成功", articles));
    }

    /**
     * 根据标签获取文章
     */
    @GetMapping("/tag/{tagName}")
    public ResponseEntity<ApiResponse<Page<ArticleDto>>> getArticlesByTag(
            @PathVariable String tagName,
            @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
        Page<ArticleDto> articles = articleService.getArticlesByTag(tagName, pageable);
        return ResponseEntity.ok(ApiResponse.success("获取标签文章列表成功", articles));
    }
}